package com.mjlf.mr.speed;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @ClassName: SpeedClient
 * @Author: majun
 * @CreateDate: 2018/12/18 10:45
 * @Version: 1.0
 * @Description: TODO
 * 2001-09-07 17:12:43	A-00NX	127.05464496950239
 */

public class SpeedClient {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        //配置实例化
        Configuration conf = new Configuration(true);

        //job实例化
        Job job = Job.getInstance(conf, "speed");

        //设置jar
        job.setJarByClass(SpeedClient.class);

        //设置输入输出
        FileInputFormat.addInputPath(job, new Path("/input/speed.txt"));
        Path outPath = new Path("/speed");
        if (outPath.getFileSystem(conf).exists(outPath)) {
            outPath.getFileSystem(conf).delete(outPath, true);
        }
        FileOutputFormat.setOutputPath(job, outPath);

        //设置map
        job.setMapperClass(SpeedMap.class);
        job.setMapOutputKeyClass(Speed.class);
        job.setMapOutputValueClass(Text.class);


        //设置partitioner
        job.setPartitionerClass(SpeedParation.class);

        //设置sort
        job.setSortComparatorClass(SpeedComparator.class);

        //设置group sort
        job.setGroupingComparatorClass(SpeedGroupComparator.class);

        //设置combiner
        job.setCombinerClass(SpeedCombiner.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        //设置reduce
        job.setReducerClass(SpeedReduce.class);

        job.setCombinerKeyGroupingComparatorClass(SpeedGroupComparator.class);

        job.waitForCompletion(true);
    }
}
